package com.nowcoder.community;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;


import java.io.PrintWriter;

/**
 * <p>Title: Kafkatest</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2022/8/21 19:53
 */


@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class Kafkatest {

    @Autowired
    private KafkaProducer kafkaProducer ;

    @Autowired
    private KafkaConsumer kafkaConsumer ;

    @Test
    public void testKafka(){
        kafkaProducer.sendMessage("test" , "你好");
        kafkaProducer.sendMessage("test" , "在吗");
        try {
            Thread.sleep(1000 * 10);
        }catch (InterruptedException e ){
            e.printStackTrace();
        }


    }

}

@Component
class KafkaProducer{

    @Autowired
    private KafkaTemplate  kafkaTemplate ;

    public void sendMessage(String topic , String content){
        kafkaTemplate.send(topic , content) ;
    }
}

@Component
class KafkaConsumer{

    @KafkaListener(topics = {"test"})
    public void handleMessage(ConsumerRecord record){
        System.out.println(record.value());
    }

}

